package com.battleship;

import java.util.Arrays;
import java.util.List;

public class GuesserTaylor implements Guesser {

    private Game game;
    private boolean gameOver = false;

    @Override
    public void play(Game game) {
        setGame(game);
        List<Integer> indexList = Arrays.asList(5, 1, 9, 7, 3);

        for (Integer colIndex : indexList) {
            if (!gameOver) {
                verticalPass(colIndex);
            }
        }
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public void verticalPass(int x) {
        int y = 1;
        int originalX = x;

        while (y <= 10 && !gameOver) {
            guess(x, y);

            if (originalX == x) {
                x++;
            } else {
                x--;
            }
            y++;
        }
    }

    protected int guess(int x, int y) {
        int guess = game.guess(new Point(x, y));
        switch (guess) {
            case 1:
                if (x < 10) {
                    guess(x + 1, y);
                }
                if (x > 1) {
                    guess(x - 1, y);
                }
                if (y > 1) {
                    guess(x, y - 1);
                }
                if (y < 10) {
                    guess(x, y + 1);
                }
                break;
            case 2:
                if (game.getShipsLeft() == 0) {
                    gameOver = true;
                }
        }
        return guess;
    }
}
